package cn.com.fig.control;

import cn.com.fig.portal.ILoginService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 在门户中访问子系统菜单或打开子系统通用方法
 * 获取用户sessionid和token，然后携带参数调用子系统相应url地址
 */
@RestController
@RequestMapping("/accessControl")
public class AccessControl {
    Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    ILoginService loginService;
    @RequestMapping("/access")
    public void access(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        String userId = (String)req.getSession().getAttribute("userId");
        String token = loginService.getToken(req);
        String sessionid = loginService.getSessionid(req);
        String url = (String)req.getParameter("accessUrl");

//        RequestDispatcher dispatcher = req.getRequestDispatcher(url + "?userId=" + userId);
//        res.setHeader("token",token);
//        res.setHeader("sessionid",sessionid);
//        dispatcher.forward(req,res);
        res.sendRedirect(url + "?userId=" + userId);
    }
}
